# Load Packages -----------------------------------------------------------
library(stargazer)


# ** Stargazer R version update if function returns errors -------------------------------------------
# Courtesy: https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53
## Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2
# # Unload stargazer if loaded
# detach("package:stargazer",unload=T)
# # Delete it
# remove.packages("stargazer")
# # Download the source
# download.file("https://cran.r-project.org/src/contrib/stargazer_5.2.3.tar.gz", destfile = "stargazer_5.2.3.tar.gz")
# # Unpack
# untar("stargazer_5.2.3.tar.gz")
# # Read the sourcefile with .inside.bracket fun
# stargazer_src <- readLines("stargazer/R/stargazer-internal.R")
# # Move the length check 5 lines up so it precedes is.na(.)
# stargazer_src[1990] <- stargazer_src[1995]
# stargazer_src[1995] <- ""
# # Save back
# writeLines(stargazer_src, con="stargazer/R/stargazer-internal.R")
# # Compile and install the patched package
# install.packages("stargazer", repos = NULL, type="source")


# Load Data ---------------------------------------------------------------
# Set file path
setwd("")

load("drop_in_ocean_data.rdata")


# Analyses ----------------------------------------------------------------
# * By Race ---------------------------------------------------------------
m_att_bivar <- lm(cjs_att ~ direct_exp*black,
                      data = cjs.df, weights = wts_bw_stk)
summary(m_att_bivar)

m_att_demog_flex <- lm(cjs_att ~ direct_exp*black + age_sc*black + man*black + 
                           educ_sc*black + inc_sc*black,
                         data = cjs.df, weights = wts_bw_stk)
summary(m_att_demog_flex)

m_att_demogContact_flex <- lm(cjs_att ~ direct_exp*black + peer.felony*black 
                                  + age_sc*black + man*black + educ_sc*black + inc_sc*black,
                           data = cjs.df, weights = wts_bw_stk)
summary(m_att_demogContact_flex)

m_att_demogRRPID_flex <- lm(cjs_att ~ direct_exp*black + rr_sc*black + pid7_dem*black +
                                  + age_sc*black + man*black + educ_sc*black + inc_sc*black,
                                  data = cjs.df, weights = wts_bw_stk)
summary(m_att_demogRRPID_flex)

m_att_all_flex <- lm(cjs_att ~ direct_exp*black + rr_sc*black + pid7_dem*black +
                           peer.felony*black + age_sc*black + man*black + educ_sc*black + inc_sc*black,
                         data = cjs.df, weights = wts_bw_stk)
summary(m_att_all_flex)

# * By Gender (Black) -----------------------------------------------------
m_gendB_att_bivar <- lm(cjs_att ~ direct_exp*man,
                      data = cjs.df, subset = black == 1, weights = wts_bw_stk)
summary(m_gendB_att_bivar)

m_gendB_att_demog_flex <- lm(cjs_att ~ direct_exp*man + age_sc*man + man*man + 
                             educ_sc*man + inc_sc*man,
                           data = cjs.df, subset = black == 1, weights = wts_bw_stk)
summary(m_gendB_att_demog_flex)

m_gendB_att_demogContact_flex <- lm(cjs_att ~ direct_exp*man + peer.felony*man 
                                  + age_sc*man + man*man + educ_sc*man + inc_sc*man,
                                  data = cjs.df, subset = black == 1, weights = wts_bw_stk)
summary(m_gendB_att_demogContact_flex)

m_gendB_att_demogRRPID_flex <- lm(cjs_att ~ direct_exp*man + rr_sc*man + pid7_dem*man +
                                  + age_sc*man +  educ_sc*man + inc_sc*man,
                                data = cjs.df, subset = black == 1, weights = wts_bw_stk)
summary(m_gendB_att_demogRRPID_flex)

m_gendB_att_all_flex <- lm(cjs_att ~ direct_exp*man + rr_sc*man + pid7_dem*man +
                           peer.felony*man + age_sc*man + educ_sc*man + inc_sc*man,
                         data = cjs.df, subset = black == 1, weights = wts_bw_stk)
summary(m_gendB_att_all_flex)


# * By Gender (White) -----------------------------------------------------
m_gendW_att_bivar <- lm(cjs_att ~ direct_exp*man,
                            data = cjs.df, subset = black == 0, weights = wts_bw_stk)
summary(m_gendW_att_bivar)

m_gendW_att_demog_flex <- lm(cjs_att ~ direct_exp*man + age_sc*man + man*man + 
                                   educ_sc*man + inc_sc*man,
                                 data = cjs.df, subset = black == 0, weights = wts_bw_stk)
summary(m_gendW_att_demog_flex)

m_gendW_att_demogContact_flex <- lm(cjs_att ~ direct_exp*man + peer.felony*man 
                                        + age_sc*man + man*man + educ_sc*man + inc_sc*man,
                                        data = cjs.df, subset = black == 0, weights = wts_bw_stk)
summary(m_gendW_att_demogContact_flex)

m_gendW_att_demogRRPID_flex <- lm(cjs_att ~ direct_exp*man + rr_sc*man + pid7_dem*man +
                                        + age_sc*man +  educ_sc*man + inc_sc*man,
                                      data = cjs.df, subset = black == 0, weights = wts_bw_stk)
summary(m_gendW_att_demogRRPID_flex)

m_gendW_att_all_flex <- lm(cjs_att ~ direct_exp*man + rr_sc*man + pid7_dem*man +
                                 peer.felony*man + age_sc*man + educ_sc*man + inc_sc*man,
                               data = cjs.df, subset = black == 0, weights = wts_bw_stk)
summary(m_gendW_att_all_flex)



# Tables -------------------------------------------------------
## table error
stargazer(m_att_bivar, m_att_demog_flex, m_att_demogContact_flex, m_att_demogRRPID_flex, m_att_all_flex,
          title = "Building Blocks Test of Direct Experiences Effect Carceral State Attitude by Race",
          dep.var.caption = "",
          dep.var.labels.include = FALSE,
          covariate.labels = c("Direct Experiences", "Experiences*Black", 
                               "Black", 
                               "Age", "Age*Black", "Man", "Black*Man",
                               "Education", "Education*Black",
                               "Income", "Income*Black",
                               "Peers with Felony Convictions", "Peers*Black",
                               "Racial Resentment", "Resentment*Black",
                               "Partisanship", "Partisanship*Black"),
          order = c(1, 10, 2, 6, 14, 7, 15, 8, 16, 9, 17,
                    3, 11, 4, 12, 5, 13,
                    18),
          model.numbers = F,
          no.space = T, header = F,
          notes = c("OLS regression results with standard errors in parentheses. Analyses weighted. Measures scaled 0-1."), 
          notes.align = "l", 
          intercept.bottom = T, initial.zero = F, label = c("pred_var_RR_bbRace"),
          type = "text",
          # out = c("tableA7.tex"),
          digits = 3, omit.stat = c("f", "adj.rsq"), df = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05))

stargazer(m_gendB_att_bivar, m_gendB_att_demog_flex, m_gendB_att_demogContact_flex, m_gendB_att_demogRRPID_flex, m_gendB_att_all_flex,
          title = "Building Blocks Test of Direct Experiences Effect Carceral State Attitude by Gender, Black Americans",
          dep.var.caption = "",
          dep.var.labels.include = FALSE,
          covariate.labels = c("Direct Experiences", "Experiences*Man",
                               "Man",
                               "Age", "Age*Man",
                               "Education", "Education*Man",
                               "Income", "Income*Man",
                               "Peers with Felony Convictions", "Peers*Man",
                               "Racial Resentment", "Resentment*Man",
                               "Partisanship", "Partisanship*Man"),
          order = c(1, 9, 2, 6, 13, 7, 14, 8, 15,
                    3, 10, 4, 11, 5, 12,
                    16),
          model.numbers = F,
          no.space = T, header = F,
          notes = c("OLS regression results with standard errors in parentheses. Analyses weighted. Measures scaled 0-1."), 
          notes.align = "l", 
          intercept.bottom = T, initial.zero = F, label = c("pred_var_RR_bbGendB"),
          type = "text",
          # out = c("tableA8.tex"),
          digits = 3, omit.stat = c("f", "adj.rsq"), df = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05))


stargazer(m_gendW_att_bivar, m_gendW_att_demog_flex, m_gendW_att_demogContact_flex, m_gendW_att_demogRRPID_flex, m_gendW_att_all_flex,
          title = "Building Blocks Test of Direct Experiences Effect Carceral State Attitude by Gender, White Americans",
          dep.var.caption = "",
          dep.var.labels.include = FALSE,
          covariate.labels = c("Direct Experiences", "Experiences*Man",
                               "Man",
                               "Age", "Age*Man",
                               "Education", "Education*Man",
                               "Income", "Income*Man",
                               "Peers with Felony Convictions", "Peers*Man",
                               "Racial Resentment", "Resentment*Man",
                               "Partisanship", "Partisanship*Man"),
          order = c(1, 9, 2, 6, 13, 7, 14, 8, 15,
                    3, 10, 4, 11, 5, 12,
                    16),
          model.numbers = F,
          no.space = T, header = F,
          notes = c("OLS regression results with standard errors in parentheses. Analyses weighted. Measures scaled 0-1."), 
          notes.align = "l", 
          intercept.bottom = T, initial.zero = F, label = c("pred_var_RR_bbGendW"),
          type = "text",
          # out = c("tableA9.tex"),
          digits = 3, omit.stat = c("f", "adj.rsq"), df = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05))


